package com.ssy.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssy.mall.exception.SsyMallException;
import com.ssy.mall.exception.SsyMallExceptionEnum;
import com.ssy.mall.model.dao.CategoryMapper;
import com.ssy.mall.model.pojo.Category;
import com.ssy.mall.model.request.AddCategoryReq;
import com.ssy.mall.model.vo.CategoryVO;
import com.ssy.mall.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 苏少有的idea
 * @date:   2021/12/23
 * @description  目录模块实现类
 */
@Service
public class  CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void add(AddCategoryReq addCategoryReq) {
        Category category = new Category();
        BeanUtils.copyProperties(addCategoryReq, category);
        Category categoryOld = categoryMapper.selectByName(addCategoryReq.getName());
        //判断是否有重名目录
        if (categoryOld != null) {
            throw new SsyMallException(SsyMallExceptionEnum.CATEGORY_EXISTED);
        }
        int count = categoryMapper.insertSelective(category);
        if (count == 0) {
            throw new SsyMallException(SsyMallExceptionEnum.CREATE_FAILED);
        }


    }

    @Override
    public void update(Category updateCategory) {
        if (updateCategory.getName() != null) {
            Category categoryOld = categoryMapper.selectByName(updateCategory.getName());
            if (categoryOld != null && !categoryOld.getId().equals(updateCategory.getId())) {
                throw new SsyMallException(SsyMallExceptionEnum.CATEGORY_EXISTED);
            }

        }
        int count = categoryMapper.updateByPrimaryKeySelective(updateCategory);
        if (count == 0) {
            throw new SsyMallException(SsyMallExceptionEnum.UPDATE_FAILED);

        }
    }

    @Override
    public void delete(Integer id) {
        //首先先查找id，如果查不到不允许山粗
        Category categoryOld = categoryMapper.selectByPrimaryKey(id);
        //查不到记录，无法删除，给出异常枚举为删除失败
        if (categoryOld == null) {
            throw new SsyMallException(SsyMallExceptionEnum.DELETE_FAILED);

        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if (count == 0) {
            throw new SsyMallException(SsyMallExceptionEnum.DELETE_FAILED);
        }

    }

    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
        //调用PageHelper.startPage前面是传入的参数，后面是orderby意思是按照什么方式排序
        PageHelper.startPage(pageNum, pageSize);//优先级 不同type按照type排序，相同type按照order_by排序
        List<Category> categoryList = categoryMapper.selectList();
        //这个PageInfo是分页依赖里有的
        PageInfo pageInfo = new PageInfo(categoryList);
        return pageInfo;


    }
    @Override
    @Cacheable(value = "listForCustomer()")
    public List<CategoryVO> listForCustomer(Integer parentId){
        List<CategoryVO> categoryVOList = new ArrayList<>();
        resursiveFindCategories(categoryVOList,parentId);
        return categoryVOList;

    }
    //递归的查找目录
    private void resursiveFindCategories(List<CategoryVO> categoryVOList,Integer parentId){
        //递归获得所有子类别，并组合成为一个"目录树"
        List<Category> categoryList = categoryMapper.selectCategoriesByParentId(parentId);
        if (!CollectionUtils.isEmpty(categoryList)) {
            for (int i = 0; i <categoryList.size() ; i++) {
                Category category = categoryList.get(i);
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category,categoryVO);
                categoryVOList.add(categoryVO);
                resursiveFindCategories(categoryVO.getChildCategory(),categoryVO.getId());
 
            }

        }
    }

}
